package study.JavaAdvanced;

import java.util.*;

/**
 * date:2025.10.23
 * title:HashMap
 * author:fzy
 */
/*-
Map集合概述
- */
//特点：散列表，键值对，无序
//键唯一，重复覆盖
public class Java_5 {
    public static void main(String[] arg) {
//        HashMapClass hashMapClass = new HashMapClass();
//        hashMapClass.putToMap("fufufu",12);
//        hashMapClass.putToMap("wangwangwang",23);
//        hashMapClass.putToMap("wangwangwang",23);
//        hashMapClass.putToMap("wangwangwang",232);
//        hashMapClass.putToMap("wangwangwang",213);
//        hashMapClass.putToMap("gmh",2323);
//        hashMapClass.printMap();
//        hashMapClass.removeMap("fufufu");
//        hashMapClass.printMap();
//       int age =  hashMapClass.getMap("gmh");
//       System.out.println("他的年龄"+age);
//       hashMapClass.toGetMap();
//        //迭代
//        //keySet方法，返回map的键数组，value，返回map的值数组
//        MapTest.MapTestVoid();
//        MapTest.MapTest2();
//        MapTest.mapTest3();
//        MapTest.mapTest4();
//        student stu = new student("fy", 123);
//        student stu2 = new student("fy2", 123);
//        StudentManager_Map studentManagerMap = new StudentManager_Map();
//        studentManagerMap.add("A", stu2);
//        studentManagerMap.add("B", stu);
//        studentManagerMap.select();
//        GradesManager.test();
        List<HashMap<String,String>> mapList = new ArrayList<>();
        for(int i = 0;i<3;i++){
            HashMap<String,String> map = new HashMap<>();
            map.put("A","1");
            map.put("B","2");
            map.put("C","3");
            mapList.add(map);
        }
        for (HashMap<String,String> map:mapList){
            Set<Map.Entry<String,String>> entries = map.entrySet();
           for (Map.Entry<String,String> entry:entries){
               System.out.println("key-> :"+entry.getKey() +",  value->:"+entry.getValue());
           }
        }
    }
}

class HashMapClass {
    private HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

    //put插入一个对象，返回值为插入对象的value，如果value为null，则返回null
    void putToMap(String key, int value) {
        try {
            hashMap.put(key, value);
        } catch (NullPointerException e) {
            throw new RuntimeException(e);
        }
    }

    //通过键移除一个map中的键值对,返回值为移除的键值对的值
    void removeMap(String key) {
        hashMap.remove(key);
    }

    int getMap(String name) {
        if (hashMap.containsKey(name)) {
            return hashMap.get(name);
        }
        return -1;

    }

    void printMap() {
        System.out.println(hashMap.toString());
    }


    //迭代
    void toGetMap() {
        for (int a = 0; a < hashMap.size(); a++) {
            //这里需要再三强调，java中object虽然为祖宗类，但是不可以强转子类，必须在转换的时候指明参数
            String[] nameArr = hashMap.keySet().toArray(new String[0]);
            for (String name : nameArr) {
                int age = getMap(name);
                System.out.println(name + "年龄为" + age);
            }

        }
    }
}

class MapTest {
    //map初始化
    static void MapTestVoid() {
        Map<String, String> map = new HashMap<>();
        //当擦混如重复的键，Map会覆盖重复的键值
        map.put("Itm1", "fufu");
        map.put("Itm2", "fufu1");
        map.put("item3", "dawd");
        map.put("Itm1", "fufu2");
        System.out.println(map);
    }

    //map基本功能
    static void MapTest2() {
        Map<String, String> map = new HashMap<>();
        map.put("1", "one");
        map.put("2", "two");
        map.put("3", "three");
        //删除
        map.remove("1");
        System.out.println("remove" + map);
        System.out.println(map.containsKey("2"));
        System.out.println(map.size());
        map.clear();
        System.out.println("clear" + map);
    }

    //map获取功能
    static void mapTest3() {
        Map<String, String> map = new HashMap<>();
        map.put("1", "one");
        map.put("2", "two");
        map.put("3", "three");
        System.out.println(map.get("1"));
        //如果这个键不存在，返回null
        System.out.println(map.get("4"));
        //获取所有键的集合
        Set<String> keySet = map.keySet();
        //获取所有值的集合
        Collection<String> valueSet = map.values();
        System.out.println(keySet);
        System.out.println(valueSet);
    }

    //map遍历
    static void mapTest4() {
        //方式1
        Map<String, String> map = new HashMap<>();
        map.put("1", "one");
        map.put("2", "two");
        map.put("3", "three");
        for (String key : map.keySet()) {
            System.out.println(map.get(key));
        }
        //方式2
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

//案例
interface dbInfo {
    void add(String key, student stu);

    void select();

    Set<Map.Entry<String, student>> getDb();
}

class StudentManager_Map implements dbInfo {
    private Map<String, student> db = new HashMap<>();

    @Override
    public void add(String key, student stu) {
        this.db.put(key, stu);
    }

    @Override
    public void select() {
        Set<String> keySet = db.keySet();
        for (String key : keySet) {
            System.out.println("Grades:" + key + ",   student:" + db.get(key).name);
        }
    }

    public void select(String str) {
        Set<Map.Entry<String, student>> entry = db.entrySet();
        for (Map.Entry<String, student> en : entry) {
            System.out.println("Grades:" + en.getKey() + ",   student:" + en.getValue().name);
        }
    }

    @Override
    public Set<Map.Entry<String, student>> getDb() {
        Set<Map.Entry<String, student>> entrySet = db.entrySet();
        return entrySet;
    }
}

//案例--键是对象
class Person_ins{
    private String name;
    private int age;


    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Person_ins personIns = (Person_ins) o;
        return  Objects.equals(name, personIns.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public Person_ins(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
enum GRADES{
    A,
    B,
    C,
    D;
}
class Db_Person{
    Map<Person_ins,GRADES> db = new HashMap<>();

    void add(Person_ins person, GRADES grades){
        this.db.put(person,grades);
    }
    void print(){
        Set<Map.Entry<Person_ins,GRADES>> entries = db.entrySet();
        for (Map.Entry<Person_ins,GRADES> entry:entries){
            System.out.println("name: "+entry.getKey().getName() + ", grades:"+entry.getValue());
        }
    }
}

class GradesManager{
    static void test(){
        Person_ins personIns_1 = new Person_ins("A",12);
        Person_ins personIns_2 = new Person_ins("B",12);
        Person_ins personIns_3 = new Person_ins("A",12);
        Db_Person dbPerson = new Db_Person();
        dbPerson.add(personIns_1,GRADES.A);
        dbPerson.add(personIns_2,GRADES.B);
        dbPerson.add(personIns_3,GRADES.D);
        dbPerson.print();

    }
}